package com.woniu.service.impl;

import com.woniu.dto.ProcessAdmissionDTO;
import com.woniu.entity.Bed;
import com.woniu.entity.InpatientApplication;
import com.woniu.entity.InpatientRecord;
import com.woniu.mapper.*;
import com.woniu.service.AdmissionService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import com.woniu.vo.AdmissionResourceVO;
import com.woniu.vo.AvailableBedVO;
import com.woniu.vo.SimpleDoctorVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class AdmissionServiceImpl implements AdmissionService {
    @Resource
    private AdmissionResourceMapper resourceMapper;
    @Resource
    private BedMapper bedMapper;
    @Resource
    private InpatientApplicationMapper applicationMapper;
    @Resource
    private InpatientRecordMapper recordMapper;
//    @Resource
//    private RechargeFeeMapper rechargeFeeMapper; // 移除财务校验
    @Override
    public ResponseUtil getAdmissionResources(Integer departmentId) {
        List<SimpleDoctorVO> doctors = resourceMapper.findDoctorsByDepartmentId(departmentId);
        List<AvailableBedVO> beds = resourceMapper.findAvailableBedsByDepartmentId(departmentId);

        AdmissionResourceVO vo = new AdmissionResourceVO();
        vo.setDoctors(doctors);
        vo.setBeds(beds);
        return ResponseUtil.get(ResponseEnum.OK, vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) //
    public ResponseUtil processAdmission(ProcessAdmissionDTO dto) {

        // 1. (保留) 悲观锁查询病床
        Bed bed;
        try {
            bed = bedMapper.selectByIdForUpdate(dto.getBedId());
            if (bed == null || bed.getStatus() != 0) {
                return ResponseUtil.get(ResponseEnum.BED_NOT_EXIST, "床位不存在或已被占用");
            }
        } catch (Exception e) {
            return ResponseUtil.get(ResponseEnum.FAIL, "锁定床位失败，请重试");
        }

        // 2. (核心修改) 校验申请单状态必须为 '0' (待确定)
        InpatientApplication application = applicationMapper.selectByPrimaryKey(dto.getApplicationId().longValue());
        if (application == null || !"0".equals(application.getIaStatus())) {
            return ResponseUtil.get(ResponseEnum.FAIL, "办理失败：该申请单不是'待确定'状态");
        }

        // 3. (保留) 创建住院记录 (inpatient_record)
        InpatientRecord record = new InpatientRecord();
        record.setHzCaseId(dto.getPatientId());
        record.setHzWorkerId(dto.getDoctorId());
        record.setHzBedId(dto.getBedId());
        record.setHzStartdate(new Date());
        record.setHzStatus(0); // 0 = 住院中
        // 押金余额设为 0，因为患者此时还未缴费
        record.setHzBalance(BigDecimal.ZERO);
        // 创建住院记录
        recordMapper.insertSelective(record);

        // 4. 更新入院申请表 (inpatient_application) 状态为 "1" (待缴费)
        InpatientApplication appUpdate = new InpatientApplication();
        appUpdate.setId(dto.getApplicationId());
        appUpdate.setIaStatus("1"); // 1 = 待缴费
        applicationMapper.updateByPrimaryKeySelective(appUpdate);

        // 5. 更新病床表 (bed) 状态为 "占用"
        Bed bedUpdate = new Bed();
        bedUpdate.setId(dto.getBedId());
        bedUpdate.setStatus(1); // 1 = 占用
        bedMapper.updateByPrimaryKeySelective(bedUpdate);

        return ResponseUtil.get(ResponseEnum.OK, "办理入院成功 (请通知患者缴押金)");
    }
}
